package com.hk.housekeeping.order.wxPay.service.impl;

import com.hk.housekeeping.order.utils.ResponseEntity;
import com.hk.housekeeping.order.wxPay.dao.PayOrderDao;
import com.hk.housekeeping.order.wxPay.dao.impl.PayOrderDaoImpl;
import com.hk.housekeeping.order.wxPay.entity.Account;
import com.hk.housekeeping.order.wxPay.service.OrderPayServ;
import com.hk.housekeeping.order.wxPay.utils.IDUtils;
import com.hk.housekeeping.order.wxPay.utils.WXPayCode;

import java.sql.SQLException;

public class OrderPayServImpl implements OrderPayServ {
    private PayOrderDao payOrderDao = new PayOrderDaoImpl();

    @Override
    public ResponseEntity<Long> addOrder(long money, Account account) throws SQLException {
        ResponseEntity<Long> entity = null;

        //账户登录校验
        if (account != null) {
            Long payId = IDUtils.createPayId();
            //调用dao层
            int count = payOrderDao.addPayOrder(money, account, payId);
            if (count != 0) {
                //添加成功，返回的是系统时间的订单号
                entity = ResponseEntity.success(payId);
            } else {
                entity = ResponseEntity.error(WXPayCode.ADD_ACCOUNT_ERROR);
            }
        } else {
            entity = ResponseEntity.error(WXPayCode.ACCOUNT_NOT_EXIST);
        }

        return entity;
    }

    @Override
    public int updatePayState(Long payId) throws SQLException {
        //调用dao
        int count = payOrderDao.updatePayOrder(payId);
        return count;
    }
}
